<?php

namespace addons\qingdong\model;

use addons\qingdong\model\Event as EventModel;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *日程提醒
 */
class Event Extends Model {
	use SoftDelete;
    // 表名,不含前缀
    protected $name = 'qingdong_event';
	const   CUSTOMER_TYPE    = 1;//客户
	const   CONTACTS_TYPE    = 2;//联系人
	const   CONTRACT_TYPE    = 3;//合同
	const   LEADS_TYPE       = 4;//线索
	const   RECEIVABLES_TYPE = 5;//回款记录
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';


	public function getCreatetimeAttr($value) {
		return date('Y-m-d H:i:s', $value);
	}


	//创建日程
	public static function createEvent($params) {
		$staff = Staff::info();
		//1：5分钟前 2：15分钟前 3：30分钟前 4：一个小时前 5：两个小时前 6：一天前 7：两天前 8：一周前
		if (isset($params['remindtype'])) {
			switch ($params['remindtype']) {
				case 1:
					$time = '-5 minutes';
				break;
				case 2:
					$time = '-15 minutes';
				break;
				case 3:
					$time = '-30 seconds';
				break;
				case 4:
					$time = '-1 hours ';
				break;
				case 5:
					$time = '-2 hours ';
				break;
				case 6:
					$time = '-1 days';
				break;
				case 7:
					$time = '-7 days';
				break;
				case 8:
					$time = '-1 week';
				break;
				default:
					$time = '-5 minutes';
			}
			$params['remind_time'] = date('Y-m-d H:i:s', strtotime($time, strtotime($params['start_time'])));
		} else {
			$params['remind_time'] = date('Y-m-d H:i:s', strtotime('+5 seconds', strtotime($params['start_time'])));
		}
		$params['staff_id'] = $staff->id;
		$Model              = new self;
        $result = $Model->allowField(true)->save($params);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($Model->getError());
		}

		return true;
	}


	/**
	 *变更状态
	 */
	public static function changeStatus($id, $status) {
		$model = new self();
		if ($model->save(['status' => $status], ['id' => $id]) == false) {
			throw new Exception('变更失败');
		}

		return true;
	}


	//根据时间获取列表
	public static function getTimeList($start_time, $end_time) {
		$staff      = Staff::info();
		$model      = new EventModel();
		$start_time = $start_time . ' 00:00:00';
		$end_time   = $end_time . ' 23:59:59';
		$events     = $model->where([
			'staff_id' => $staff->id
		])->where(function ($query) use ($start_time, $end_time) {
			$query->where(['start_time' => ['between', [$start_time, $end_time]]])->whereOr([
				'end_time' => [
					'between',
					[
						$start_time,
						$end_time
					]
				]
			]);
		})->select();
		$data       = [];
		$start_time = strtotime($start_time);
		$end_time   = strtotime($end_time);
		for ($start_time; $start_time <= $end_time; $start_time = strtotime('+1 day', $start_time)) {
			$time        = date('Y-m-d', $start_time);
			$data[$time] = [];
			foreach ($events as $k => $v) {
				if (strtotime($v['start_time']) <= $start_time + 86400 - 1 && strtotime($v['end_time']) >= $start_time) {
					$data[$time][] = $v;
				}
			}
		}
		foreach ($data as &$times) {
			foreach ($times as &$ves) {
				$ves['start_time'] = date('Y-m-d H:i', strtotime($ves['start_time']));
				$ves['end_time']   = date('Y-m-d H:i', strtotime($ves['end_time']));
			}
		}

		return $data;
	}


	//负责人
	public function staff() {
		return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,name');
	}
}
